package partB;

import misc.not_Legal_Exception;

import evolution_superclasses.Phenotype;
import binary_evolution.Binary_Genotype;

public class B_Genotype extends Binary_Genotype {

	public B_Genotype(int size) {
		super(size);
	}


	@Override
	public Phenotype generatePhenotype(){
		set_phenotype(new B_Phenotype(this));
		return get_phenotype();
	}
	
	@Override
	public String toString() {
		String s = "";
		for(int i = 0; i < get_genSize(); i++){
			s += (_binaryArray[i]?'1':'0')+((i%4==3)?" ":"");
		}
		return s;
	}

	@Override
	public void createGenotype(int size) throws not_Legal_Exception {
		if(size%4!=0)throw new not_Legal_Exception("B genotypes must be a multiple of 4");
		_binaryArray = new boolean[size];
		
		for (int i = 0; i < size;){
			int value = _rand.nextInt(10);
			_binaryArray[i++] = (value&1)==1;
			_binaryArray[i++] = (value&2)==2;
			_binaryArray[i++] = (value&4)==4;
			_binaryArray[i++] = (value&8)==8;
		}
	}
	
	public void validate(){
		int value;
		for(int i = 0; i < get_genSize();){
			value = (_binaryArray[i++]?1:0)+(_binaryArray[i++]?2:0)+(_binaryArray[i++]?4:0)+(_binaryArray[i++]?8:0);
			if(value>10){//Mutation has tried to increase value, set to 10
				_binaryArray[i-4]=false;
				_binaryArray[i-3]=true;
				_binaryArray[i-2]=false;
				_binaryArray[i-1]=true;
			}
		}
	}

}
